package model;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;

import enums.WxuserRole;
import model.base.BaseWxuser;

@SuppressWarnings("serial")
public class Wxuser extends BaseWxuser<Wxuser> {
    public static final Wxuser dao = new Wxuser().dao();
    
    public String getCompanyName() {
		if (isCompanyRole()) {
			Company company = Company.dao.findById(getCompanyId());
			return null == company ? null : company.getName() ;
		}
		if (isSeviserRole() || isStaffRole() ) {
			Servicer servicer = Servicer.dao.findById(getCompanyId());
			return null == servicer ? null : servicer.getName() ;
		}
		return null ;
	}

	public Wxuser findFirstByToken(String token){
        return this.findFirst(" SELECT tw.* FROM t_wxuser tw WHERE tw.token = ?  LIMIT 1 " , token);
    }

	public Wxuser findFirstByMobileAndRole(String mobile , Integer role){
        return this.findFirst(" SELECT tw.* FROM t_wxuser tw WHERE tw.mobile = ? AND tw.role = ?  LIMIT 1 " , mobile , role);
    }

	public List<Wxuser> findByMobile(String mobile){
        return this.find(" SELECT tw.* FROM t_wxuser tw WHERE tw.mobile = ?  " , mobile);
    }
	
	public void saveForCompany(String mobile , int role , Long companyId) {
		Wxuser wxuser = Wxuser.dao.findFirstByCompanyIdAndRole(companyId, role);
		if (null != wxuser) {
			wxuser.setMobile(mobile);
			wxuser.update();
			return ;
		}
		wxuser = new Wxuser();
		wxuser.setMobile(mobile);
		wxuser.setRole(role);
		wxuser.setCompanyId(companyId);
		wxuser.save();
	}
	
	public void saveForStaff(String mobile , int role , Long companyId) {
		Wxuser wxuser = Wxuser.dao.findFirstByMobileAndRoleAndCompanyId(mobile, role, companyId);
		if (null != wxuser) {
			return ;
		}
		wxuser = new Wxuser();
		wxuser.setMobile(mobile);
		wxuser.setRole(role);
		wxuser.setCompanyId(companyId);
		wxuser.save();
	}

	public Wxuser findFirstByMobileAndRoleAndCompanyId(String mobile , Integer role , Long companyId){
        return this.findFirst(" SELECT tw.* FROM t_wxuser tw WHERE tw.mobile = ? AND tw.role = ? AND tw.companyId = ?  LIMIT 1 " , mobile , role , companyId);
    }

	public Wxuser findFirstByCompanyIdAndRole(Long companyId , Integer role){
        return this.findFirst(" SELECT tw.* FROM t_wxuser tw WHERE tw.companyId = ? AND tw.role = ?  LIMIT 1 " , companyId , role);
    }
	
	public void deleteStaff(Staff staff) {
		Db.update( " DELETE FROM t_wxuser WHERE mobile = ? AND companyId = ? AND role = ? " , staff.getMobile() , staff.getServicerId() , WxuserRole.getStaff() );
	}
	
	public void deleteCompany(Company company) {
		Db.update( " DELETE FROM t_wxuser WHERE mobile = ? AND companyId = ? AND role = ? " , company.getMobile() , company.getId() , WxuserRole.getCompany() );
	}
	
	public void deleteUser(String mobile , Long companyId, int role) {
		Db.update( " DELETE FROM t_wxuser WHERE mobile = ? AND companyId = ? AND role = ? " , mobile , companyId, role );
	}
	
	public void deleteServicer(Servicer servicer) {
		Db.update( " DELETE FROM t_wxuser WHERE companyId = ? AND (role = ? OR role = ?) " , servicer.getId() , WxuserRole.getSeviser() , WxuserRole.getStaff() );
	}
}
